'use strict';

/**
 * @ngdoc service
 * @name jgappApp.usertaskService
 * @description
 * # usertaskService
 * Service in the jgappApp.
 */
angular.module('jgappApp')
    .service('UsertaskService', function($http, Util, $filter, Modulemap) {
        // AngularJS will instantiate a singleton by calling "new" on this function
        var UsertaskService = function(url, options) {
            this.items = [];
            this.busy = false;
            this.limit = 10;
            this.start = 0;
            this.pageNo = 0;
            this.noData = true;
            this.tip = "";
            this.url = url;
            this.options = options || {};
            this.nextPage();
            this.count = 0;
        };
        UsertaskService.prototype.nextPage = function() {
            if (!this.noData) {
                return;
            }
            if (this.busy) {
                return;
            }
            this.busy = true;
            this.start = (this.pageNo++) * this.limit;
            this.options.start = this.start
            this.options.limit = this.limit
            $http.get(this.url, {
                params: this.options
            }).
            then(function(data, status, headers, config) {
                data=data.data;if (data.logout) {
                    Util.logout();
                    return;
                }
                if (data.success) {
                    // 如果没有数据则返回
                    if (data.dataList && data.dataList.length > 0) {
                        var items = data.dataList;
                        this.count = data.totalCount;
                        // this.items.push(items[i]);
                        this.listFactory(items);
                        if (this.limit > this.items.length) {
                            this.noData = false;
                            if (this.items.length < 1) {
                                this.tip = "没有数据";
                            }
                        }

                    } else {
                        this.noData = false;
                        this.tip = "没有数据";
                    }
                } else {
                    this.noData = false;
                    this.tip = "没有数据";
                }
                this.busy = false;
            }.bind(this));
        };
        // 构造带有日期的数组函数
        UsertaskService.prototype.listFactory = function(data) {
            var dataitem;
            var exsit = false; //默认不存在
            // var date = $filter("dateFmt")(data.createTime)
            var that = this;
            data = data || [];
            data.forEach(function(item, index) {
                Modulemap.getviewData(item, item.formKey);
            });
            this.items = this.items.concat(data);
        };
        return UsertaskService;

    });